package gui;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.BoxLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JList;
import javax.swing.JScrollPane;

import model.DateUtil;
import model.Subware;
import model.SystemDate;

import service.Service;

public class MainFrame extends JFrame {
	private JPanel pnlNorth;
	private JButton btnOpretBehandling;
	private JButton btnOpretDelbehandling;
	private JPanel pnlSouth;
	private JButton btnCheckVareInd;
	private JButton btnCheckVareUd;
	private JPanel pnlCenter;
	private JList lstOfDoneWare;
	private JScrollPane scrollPane;
	private Controller controller = new Controller();
	private Service service = Service.getInstance();
	private JButton btnProcess;

	public MainFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("Lagersystem");

		pnlNorth = new JPanel();
		getContentPane().add(pnlNorth, BorderLayout.NORTH);
		pnlNorth.setLayout(new BoxLayout(pnlNorth, BoxLayout.X_AXIS));

		btnOpretDelbehandling = new JButton("Opret Delbehandling");
		pnlNorth.add(btnOpretDelbehandling);
		btnOpretDelbehandling.addActionListener(controller);

		btnOpretBehandling = new JButton("Opret Behandling");
		pnlNorth.add(btnOpretBehandling);
		btnOpretBehandling.addActionListener(controller);

		pnlSouth = new JPanel();
		getContentPane().add(pnlSouth, BorderLayout.SOUTH);
		pnlSouth.setLayout(new BoxLayout(pnlSouth, BoxLayout.X_AXIS));

		btnCheckVareInd = new JButton("Check vare ind");
		pnlSouth.add(btnCheckVareInd);
		btnCheckVareInd.addActionListener(controller);

		btnCheckVareUd = new JButton("Check vare ud");
		pnlSouth.add(btnCheckVareUd);

		btnProcess = new JButton("Forarbejd");
		pnlSouth.add(btnProcess);
		btnCheckVareUd.addActionListener(controller);
		btnProcess.addActionListener(controller);

		pnlCenter = new JPanel();
		getContentPane().add(pnlCenter, BorderLayout.CENTER);
		pnlCenter.setLayout(new BoxLayout(pnlCenter, BoxLayout.X_AXIS));

		scrollPane = new JScrollPane();
		pnlCenter.add(scrollPane);

		lstOfDoneWare = new JList();
		scrollPane.setViewportView(lstOfDoneWare);
		UpdateList();

		this.pack();
	}
	private void UpdateList(){
		lstOfDoneWare.setListData(service.getSubwares().toArray());
	}

	private class Controller implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == btnOpretBehandling) {
				CreateTreatment ct = new CreateTreatment();
				ct.setVisible(true);
			} else if (e.getSource() == btnOpretDelbehandling) {
				CreateSubtreatment cs = new CreateSubtreatment();
				cs.setVisible(true);
			} else if (e.getSource() == btnCheckVareInd) {
				CheckIn ci = new CheckIn();
				ci.setVisible(true);
			} else if (e.getSource() == btnCheckVareUd) {
				CheckOut co = new CheckOut();
				co.setVisible(true);
			} else if (e.getSource() == btnProcess) {
				
				if(lstOfDoneWare.getSelectedValue() != null){
					
				Subware subware = (Subware)lstOfDoneWare.getSelectedValue();
				service.startSubtreatments(subware,
						DateUtil.dateToString(SystemDate.getInstance().getDate()),DateUtil.dateToString(SystemDate
								.getInstance().getDate()), service.getLocations()
								.get(0));
				} else { 
					//DO NOTHING
				}
			}

		}
	}

}
